﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using NetJsWire.Direct.attributes;
using NetJsWire.Direct.helper;
using Newtonsoft.Json;

namespace NetJsWire.Direct
{
    /// <summary>
    ///   This class represents a container for a series of related Ext.Direct methods.
    /// </summary>
    internal class DirectAction
    {

        private readonly Dictionary<string, DirectMethod> methods;

        /// <summary>
        ///   Creates an instance of this object.
        /// </summary>
        /// <param name = "type">The type of the object.</param>
        internal DirectAction(Type type)
        {
            Type = type;
            methods = new Dictionary<string, DirectMethod>();
            Configure();
        }

        /// <summary>
        ///   Gets the type of the action.
        /// </summary>
        internal Type Type
        {
            get;
            private set;
        }

        /// <summary>
        ///   Gets the name of the action.
        /// </summary>
        internal string Name
        {
            get
            {
                return Type.Name;
            }
        }

        /// <summary>
        ///   Checks whether a particular type is an Ext.Direct action.
        /// </summary>
        /// <param name = "type">The type to check.</param>
        /// <returns>True if the type is an Ext.Direct action.</returns>
        internal static bool IsAction(Type type)
        {
            return Utility.HasAttribute(type, typeof(DirectActionAttribute));
        }

        /// <summary>
        ///   Write API JSON.
        /// </summary>
        /// <param name = "jw">The JSON writer.</param>
        internal void Write(JsonTextWriter jw)
        {
            jw.WritePropertyName(Name);
            jw.WriteStartArray();
            foreach (var method in methods.Values)
            {
                method.Write(jw);
            }
            jw.WriteEndArray();
        }

        internal void WriteDocumentation(JsonTextWriter jw)
        {
            jw.WriteRaw("var " + Name + " = ");
            jw.WriteStartObject();
            foreach (var method in methods.Values)
            {
                method.Write(jw);
            }
            jw.WriteEndObject();
        }

        private void Configure()
        {
            foreach (var mi in Type.GetMethods())
            {
                if (DirectMethod.IsMethod(mi))
                {
                    var methType = ((DirectMethodAttribute)mi.GetCustomAttributes(typeof(DirectMethodAttribute), true)[0]).MethodType;
                    if (methType == DirectMethodType.Hybrid)
                    {
                        methods.Add(mi.Name, new DirectMethod(mi, DirectMethodType.Normal, this));
                        methods.Add(mi.Name + "_Form", new DirectMethod(mi, DirectMethodType.Form, this, mi.Name + "_Form"));
                    }
                    else
                    {
                        methods.Add(mi.Name, new DirectMethod(mi, methType, this));
                    }
                }
            }
        }

        internal DirectMethod GetMethod(string name)
        {
            return methods[name];
        }

    }
}
